﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace PS.Framework.EventLogger
{
    internal class EventLogger : ILogEngine
    {
        private LoggerConfiguration _configuration;
        private static EventLog _log;
        public LoggerConfiguration Configuration
        {
            get
            {
                return this._configuration;
            }
            set
            {
                if (this._configuration == value)
                {
                    return;
                }
                this._configuration = value;
                EventLogger._log = new EventLog();
                EventLogger._log.Log = this._configuration.EventLog;
                EventLogger._log.Source = this._configuration.EventSource;
            }
        }

        public void WriteLog(string message, Exception exc, LogEntryType logEntryType)
        {
            EventLogEntryType type = (EventLogEntryType)0;
            switch (logEntryType)
            {
                case LogEntryType.Error:
                    {
                        type = EventLogEntryType.Error;
                        break;
                    }
                case LogEntryType.SuccessAudit:
                    {
                        type = EventLogEntryType.SuccessAudit;
                        break;
                    }
                case LogEntryType.Information:
                    {
                        type = EventLogEntryType.Information;
                        break;
                    }
            }
            if (exc != null)
            {
                message = message + "\r\n\r\n" + exc.ToString();
            }
            EventLogger._log.WriteEntry(message, type);
        }

        public LogEntry[] GetLogEntries()
        {
            List<LogEntry> list = new List<LogEntry>();
            foreach (EventLogEntry eventLogEntry in EventLogger._log.Entries)
            {
                LogEntry logEntry = new LogEntry();
                EventLogEntryType entryType = eventLogEntry.EntryType;
                switch (entryType)
                {
                    case EventLogEntryType.Error:
                        {
                            logEntry.LogEntryType = LogEntryType.Error;
                            break;
                        }
                    case EventLogEntryType.Warning:
                        {
                            logEntry.LogEntryType = LogEntryType.Warning;
                            break;
                        }
                    case (EventLogEntryType)3:
                        {
                            goto IL_7B;
                        }
                    case EventLogEntryType.Information:
                        {
                            logEntry.LogEntryType = LogEntryType.Information;
                            break;
                        }
                    default:
                        {
                            if (entryType != EventLogEntryType.SuccessAudit)
                            {
                                goto IL_7B;
                            }
                            logEntry.LogEntryType = LogEntryType.SuccessAudit;
                            break;
                        }
                }
            IL_82:
                int num = eventLogEntry.Message.IndexOf("\r\n\r\n");
                logEntry.Message = eventLogEntry.Message.Substring(0, num);
                logEntry.DateTime = eventLogEntry.TimeWritten;
                logEntry.Exception = eventLogEntry.Message.Remove(0, num);
                list.Add(logEntry);
                continue;
            IL_7B:
                logEntry.LogEntryType = LogEntryType.Unknown;
                goto IL_82;
            }
            return list.ToArray();
        }
        public void Dispose()
        {
            EventLogger._log.Dispose();
        }
    }
}
